package design.pattern.singleton;


import java.util.ArrayList;
import java.util.List;

/**
 * 懒汉式-双检锁模式
 */
public class LazySingleton {


	private LazySingleton(){
		System.out.println("LazySingleton is create");
	}

	private static LazySingleton instance = null;

	public static void main(String[] args) throws InterruptedException {

		LazySingleton instance1 = LazySingleton.getInstance();

			Thread t = new Thread(()->{
				int j = 0;
					for (int a = 0; a < 10000; a++) {
						j = j + 1;
					}

			});
			Thread t1 = new Thread(()->{
				int j = 0;
				for (int a = 0; a < 10000; a++) {
					j = j + 1;
				}

		});
			t1.start();
			t.start();
			t1.join();
			t.join();
	}

//	public static synchronized LazySingleton getInstance1(){
//		if (instance==null){
//			instance = new LazySingleton();
//		}
//		return instance;
//	}

	public static  LazySingleton getInstance() {
		if (instance !=null){
			return instance;
		}
		synchronized(LazySingleton.class){
			if (instance !=null){
				return instance;
			}
			instance=new LazySingleton();
		}
		return instance;
	}
}
